package com.sixmac.service.impl;

import com.sixmac.controller.common.CommonController;
import com.sixmac.core.Constant;
import com.sixmac.dao.AccountDao;
import com.sixmac.dao.AppUserDao;
import com.sixmac.dao.UserAccountDao;
import com.sixmac.dao.WxUserDao;
import com.sixmac.entity.Account;
import com.sixmac.entity.AppUser;
import com.sixmac.entity.UserAccount;
import com.sixmac.entity.WxUser;
import com.sixmac.service.AccountService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19 0019 上午 10:02.
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private UserAccountDao userAccountDao;

    @Autowired
    private AppUserDao appUserDao;

    @Autowired
    private WxUserDao wxUserDao;

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Page<Account> find(int pageNum, int pageSize) {
        return accountDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Account> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Account getById(int id) {
        return accountDao.findOne(id);
    }

    @Override
    public Account deleteById(int id) {
        Account account = getById(id);
        accountDao.delete(account);
        return account;
    }

    @Override
    public Account create(Account account) {
        return accountDao.save(account);
    }

    @Override
    public Account update(Account account) {
        return accountDao.save(account);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Account> findListNew() {
        return accountDao.findListNew(CommonController.getOldDate());
    }

    @Override
    public List<Account> findListById(Integer userId, Integer type) {
        List<UserAccount> list = userAccountDao.findListByIdAndType(userId, type);
        List<Account> accountList = new ArrayList<Account>();

        for (UserAccount userAccount : list) {
            accountList.add(userAccount.getAccount());
        }

        return accountList;
    }

    @Override
    public Page<Account> page(final String num, final String mobile, final String name, Integer pageNum, Integer pageSize) {
        Page<Account> page = accountDao.findAll(new Specification<Account>() {
            @Override
            public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();

                if (StringUtils.isNotEmpty(num)) {
                    Predicate pre = cb.like(root.get("num").as(String.class), "%" + num + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotEmpty(mobile)) {
                    Predicate pre = cb.like(root.get("mobile").as(String.class), "%" + mobile + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotEmpty(name)) {
                    Predicate pre = cb.like(root.get("name").as(String.class), "%" + name + "%");
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));

        return page;
    }

    @Override
    public List<AppUser> findAppUserListByAccountId(Integer accountId) {
        List<UserAccount> list = userAccountDao.findListByParams(accountId, Constant.ACCOUNT_STATUS_App);
        List<AppUser> appUserList = new ArrayList<AppUser>();

        for (UserAccount userAccount : list) {
            appUserList.add(appUserDao.findOne(userAccount.getUserId()));
        }
        return appUserList;
    }

    @Override
    public List<WxUser> findWxUserListByAccountId(Integer accountId) {
        List<UserAccount> list = userAccountDao.findListByParams(accountId, Constant.ACCOUNT_STATUS_WeiXin);
        List<WxUser> wxUserList = new ArrayList<WxUser>();
        for (UserAccount userAccount : list) {
            wxUserList.add(wxUserDao.findOne(userAccount.getUserId()));
        }
        return wxUserList;
    }
}